/*
 * Copyright (c) 2020 WildFireChat. All rights reserved.
 */

package cn.wildfire.chat.kit.search;

import android.content.Intent;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnTextChanged;
import cn.wildfire.chat.kit.R;
import cn.wildfire.chat.kit.R2;
import cn.wildfire.chat.kit.UserListSimpleAdapter;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfire.chat.kit.WfcBaseNoToolbarActivity;
import cn.wildfire.chat.kit.contact.UserListAdapter;
import cn.wildfire.chat.kit.conversation.ConversationMemberAdapter;
import cn.wildfire.chat.kit.group.GroupViewModel;
import cn.wildfire.chat.kit.search.module.ConversationMessageSearchModule;
import cn.wildfire.chat.kit.user.UserInfoActivity;
import cn.wildfire.chat.kit.user.UserViewModel;
import cn.wildfire.chat.kit.widget.SearchView;
import cn.wildfirechat.model.Conversation;
import cn.wildfirechat.model.GroupInfo;
import cn.wildfirechat.model.GroupMember;
import cn.wildfirechat.model.UserInfo;

public class SearchMemberActivity extends WfcBaseActivity implements UserListSimpleAdapter.OnMemberClickListener {
    private GroupViewModel groupViewModel;
    protected UserListSimpleAdapter userListAdapter;
    @BindView(R2.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R2.id.search_view)
    SearchView searchView;
    private GroupInfo groupInfo;
    private List<UserInfo> members;

    @Override
    protected int contentLayout() {
        return R.layout.activity_search_member;
    }

    @Override
    protected void afterViews() {
        searchView.setOnQueryTextListener(this::doSearch);
        groupInfo = getIntent().getParcelableExtra("groupInfo");
        groupViewModel = ViewModelProviders.of(this).get(GroupViewModel.class);
        groupViewModel.getGroupMembersLiveData(groupInfo.target, true)
                .observe(this, groupMembers -> {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            userListAdapter = new UserListSimpleAdapter(SearchMemberActivity.this);
                            userListAdapter.setOnMemberClickListener(SearchMemberActivity.this);
                            mRecyclerView.setAdapter(userListAdapter);
                            List<String> memberIds = new ArrayList<>();
                            for (GroupMember member : groupMembers) {
                                memberIds.add(member.memberId);
                            }
                            members = UserViewModel.getUsers(memberIds, groupInfo.target);
                            userListAdapter.setData(members);
                        }
                    });
                });
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    private void doSearch(String keyword) {
        List<UserInfo> data = new ArrayList<>();
        if (members==null){
            return;
        }
        if (!TextUtils.isEmpty(keyword)) {
            for (UserInfo d:members) {
                if (d.displayName.contains(keyword)){
                    data.add(d);
                }
            }
            userListAdapter.setData(data);
        } else {
            userListAdapter.setData(members);
        }
    }

    @Override
    public void onUserMemberClick(UserInfo userInfo) {
        Intent intent = new Intent(this, UserInfoActivity.class);
        intent.putExtra("userInfo", userInfo);
        intent.putExtra("groupId", groupInfo.target);
        startActivity(intent);
    }
}
